namespace CISLib
{
    using System;
    using System.Collections;
    using System.Data;
    using System.Data.SqlClient;

    public class ManageTree
    {
        public static int GetNodeChildCount(Guid id)
        {
            DBAccess MyDB = new DBAccess();
            MyDB.DBOpen();
            MyDB.AddParameters(id, "ParentId", SqlDbType.UniqueIdentifier);
            string strSQL = "select count(*) from TB_CustomerOrgTree where ParentId = @ParentId";
            MyDB.DBSetSelect(strSQL);
            object result = MyDB.DBExecuteScalar();
            MyDB.DBClose();
            int resultCount = 0;
            if (result != null)
            {
                resultCount = Convert.ToInt32(result);
            }
            return resultCount;
        }

        public static Guid[] GetNodesChildIdAry(Guid nodeId)
        {
            SqlDataReader MyReader = null;
            ArrayList List = new ArrayList();
            DBAccess MyDB = new DBAccess();
            MyDB.DBOpen();
            MyDB.AddParameters(nodeId, "ParentId", SqlDbType.UniqueIdentifier);
            string strSQL = "select NodeId from TB_CustomerOrgTree where ParentId = @ParentId order by NodeSort";
            MyDB.DBSetSelect(strSQL);
            MyDB.DBExcuteReader();
            MyReader = MyDB.DBReader;
            while (MyReader.Read())
            {
                Guid id = (Guid) MyReader[0];
                List.Add(id);
            }
            MyDB.DBClose();
            return (Guid[]) List.ToArray(typeof(Guid));
        }

        public static Guid[] GetNodesChildIdAry(Guid nodeId, Guid companyId)
        {
            SqlDataReader MyReader = null;
            ArrayList List = new ArrayList();
            DBAccess MyDB = new DBAccess();
            MyDB.DBOpen();
            MyDB.AddParameters(nodeId, "ParentId", SqlDbType.UniqueIdentifier);
            MyDB.AddParameters(companyId, "CompanyId", SqlDbType.UniqueIdentifier);
            string strSQL = "select NodeId from TB_CustomerOrgTree where ParentId = @ParentId and CompanyId = @CompanyId order by NodeSort";
            MyDB.DBSetSelect(strSQL);
            MyDB.DBExcuteReader();
            MyReader = MyDB.DBReader;
            while (MyReader.Read())
            {
                Guid id = (Guid) MyReader[0];
                List.Add(id);
            }
            MyDB.DBClose();
            return (Guid[]) List.ToArray(typeof(Guid));
        }

        public static CisTreeNode GetNodeViaId(Guid id)
        {
            SqlDataReader MyReader = null;
            CisTreeNode node = new CisTreeNode();
            DBAccess MyDB = new DBAccess();
            MyDB.DBOpen();
            string strSQL = "select * from TB_CustomerOrgTree where NodeId = @NodeId";
            MyDB.AddParameters(id, "NodeId", SqlDbType.UniqueIdentifier);
            MyDB.DBSetSelect(strSQL);
            MyDB.DBExcuteReader();
            MyReader = MyDB.DBReader;
            if (MyReader.Read())
            {
                Guid nodeid = MyReader.GetGuid(0);
                node.NodeId = nodeid;
                node.ParentId = MyReader.GetGuid(1);
                node.NodeName = MyReader.GetString(2);
                node.NodeTitle = MyReader.GetString(4);
                node.NodePart = MyReader.GetString(5);
                node.NodeSort = MyReader.GetInt32(6);
            }
            MyDB.DBClose();
            return node;
        }

        public static CisTreeNode GetNodeViaId(Guid id, Guid companyId)
        {
            SqlDataReader MyReader = null;
            CisTreeNode node = new CisTreeNode();
            DBAccess MyDB = new DBAccess();
            MyDB.DBOpen();
            string strSQL = "select * from TB_CustomerOrgTree where NodeId = @NodeId and CompanyId = @CompanyId";
            MyDB.AddParameters(id, "NodeId", SqlDbType.UniqueIdentifier);
            MyDB.AddParameters(companyId, "CompanyId", SqlDbType.UniqueIdentifier);
            MyDB.DBSetSelect(strSQL);
            MyDB.DBExcuteReader();
            MyReader = MyDB.DBReader;
            if (MyReader.Read())
            {
                Guid nodeid = MyReader.GetGuid(0);
                node.NodeId = nodeid;
                node.ParentId = MyReader.GetGuid(1);
                node.NodeName = MyReader.GetString(2);
                node.NodeTitle = MyReader.GetString(4);
                node.NodePart = MyReader.GetString(5);
                node.NodeSort = MyReader.GetInt32(6);
            }
            MyDB.DBClose();
            return node;
        }

        public static void InsertNewNode(string name, Guid parentId, Guid companyId, string title, string part, int sort)
        {
            Guid id = Guid.NewGuid();
            DBAccess MyDB = new DBAccess();
            MyDB.DBOpen();
            MyDB.AddParameters(name, "NodeName", SqlDbType.VarChar);
            MyDB.AddParameters(title, "NodeTitle", SqlDbType.VarChar);
            MyDB.AddParameters(parentId, "ParentId", SqlDbType.UniqueIdentifier);
            MyDB.AddParameters(id, "NodeId", SqlDbType.UniqueIdentifier);
            MyDB.AddParameters(companyId, "CompanyId", SqlDbType.UniqueIdentifier);
            MyDB.AddParameters(part, "NodePart", SqlDbType.VarChar);
            MyDB.AddParameters(sort, "NodeSort", SqlDbType.Int);
            string strSQL = "Insert into TB_CustomerOrgTree (NodeName,ParentId,NodeId,CompanyId,NodeTitle,NodePart,NodeSort) values (@NodeName,@ParentId,@NodeId,@CompanyId,@NodeTitle,@NodePart,@NodeSort)";
            MyDB.DBSetSelect(strSQL);
            MyDB.DBExcuteNonQuery();
            MyDB.DBClose();
        }

        public static bool IsExitNodeChild(Guid id)
        {
            DBAccess MyDB = new DBAccess();
            MyDB.DBOpen();
            MyDB.AddParameters(id, "ParentId", SqlDbType.UniqueIdentifier);
            string strSQL = "select * from TB_CustomerOrgTree where ParentId = @ParentId";
            MyDB.DBSetSelect(strSQL);
            if (MyDB.DBExecuteScalar() == null)
            {
                MyDB.DBClose();
                return false;
            }
            MyDB.DBClose();
            return true;
        }

        public static void RemoveNodeViaId(Guid id)
        {
            DBAccess MyDB = new DBAccess();
            MyDB.DBOpen();
            MyDB.AddParameters(id, "NodeId", SqlDbType.UniqueIdentifier);
            string strSQL = "Delete From TB_CustomerOrgTree where NodeId = @NodeId and parentId <> '00000000-0000-0000-0000-000000000000'";
            MyDB.DBSetSelect(strSQL);
            MyDB.DBExcuteNonQuery();
            MyDB.DBClose();
        }

        public static void UpdateNodeNameAndTitleViaId(string name, string title, Guid nodeId, string part, int sort)
        {
            DBAccess MyDB = new DBAccess();
            MyDB.DBOpen();
            MyDB.AddParameters(name, "NodeName", SqlDbType.VarChar);
            MyDB.AddParameters(title, "NodeTitle", SqlDbType.VarChar);
            MyDB.AddParameters(nodeId, "NodeId", SqlDbType.UniqueIdentifier);
            MyDB.AddParameters(part, "NodePart", SqlDbType.VarChar);
            MyDB.AddParameters(sort, "NodeSort", SqlDbType.Int);
            string strSQL = "Update TB_CustomerOrgTree set NodeName = @NodeName,NodeTitle=@NodeTitle,NodePart=@NodePart,NodeSort=@NodeSort where NodeId = @NodeId";
            MyDB.DBSetSelect(strSQL);
            MyDB.DBExcuteNonQuery();
            MyDB.DBClose();
        }

        public static void UpdateNodeParentIdViaNodeId(Guid nodeId, Guid nodeParentId)
        {
            DBAccess MyDB = new DBAccess();
            MyDB.DBOpen();
            MyDB.AddParameters(nodeParentId, "ParentId", SqlDbType.UniqueIdentifier);
            MyDB.AddParameters(nodeId, "NodeId", SqlDbType.UniqueIdentifier);
            string strSQL = "Update TB_CustomerOrgTree set ParentId = @ParentId where NodeId = @NodeId";
            MyDB.DBSetSelect(strSQL);
            MyDB.DBExcuteNonQuery();
            MyDB.DBClose();
        }
    }
}

